const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const pay = require('../../services/pay.js');
const moment = require('../../lib/moment/moment.min.js');
var app = getApp();
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bill: {},
    isLoad: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
  },
  initDetail() {
    app.postForm(api.GetPayRent, { retry: 3 }).then(rsp => {
      var detail = rsp.data;
      if (detail) {
        Object.keys(detail).forEach(function (k, n) {
          if (['totalCost', 'rentAmount', 'networkFee', 'electricity', 'waterFee', 'hygieneFee', 'otherFees'].indexOf(k) != -1) {
            detail[k] = (Number(detail[k]) / 100).toFixed(2);
          }
        });
        //标题时间
        detail.tltDate = moment(detail.createTime).format('YYYY年M月');
        //房租周期
        detail.rentCycle = moment(detail.createTime).format('YYYY年MM月DD日') + '-' + moment(detail.cycle).format('M月DD日');
        //出账日
        detail.accDate = moment(detail.createTime).format('YYYY年MM月DD日');
        //交租日
        detail.cycle = moment(detail.cycle).format('YYYY年MM月DD日');
        that.setData({
          isLoad: true,
          bill: detail
        });
      }
    }).catch((err) => {
      console.log(err);
    });
  },
  payRent(){
    if (!app.isTouristRent){
      util.confirm.modal('请您找房东登记个人信息!');
      return;
    }
    if (!that.data.bill.isReading){
      util.confirm.modal('您的房东还未记录当前水电,请等待房东抄录后再操作!');
      return;
    }
    util.confirm.openExt('您确定了吗?',function(){
      pay.payOrder(parseInt(that.data.bill.id)).then(res => {
        if (res == 'fail') reutrn;
        wx.navigateTo({
          url: '/pages/payResult/payResult?orderId=' + that.data.bill.id,
        })
      }).catch(res => {
        if (!res.isLogin) {
          if (res.errmsg) {
            util.confirm.modal(res.errmsg, '注意', function () {
              that.initDetail();
            });
          } else {
            util.toast.fail('支付失败');
          }
        }
      });
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that.initDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    that.initDetail();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})